#pragma once
#include <map> 
using namespace std;
#include "AnsiCommand.h"

[event_source(native)]
class CAnsiInterpreter
{
	enum State 
    { 
        Text,			// Reading text
        WaitingForLBR,	// ESC read, waiting for [
        Escaped			// Parsing ANSI command
    };

public:
	CAnsiInterpreter(void);
	~CAnsiInterpreter(void);

	void ProcessInput(unsigned char* value, int count);

	__event void OnTextReceived(unsigned char* payloadBuffer, int payloadCount);
	__event void OnAnsiCommandReceived(CAnsiCommand* command);

protected:
	static map<char, CAnsiCommand::Action> letterToActionMapping;

	static const int defaultBufferSize = 4096;
	static const int maxParameterLength = 256;

	State state; // State for tracking ANSI command parsing

	unsigned char* textBuffer; // Tracking text reading
    int textLength;

	int* parameterBuffer; // Storing parameter parsing
    int parameterLength;

	int parameterHolder;
	bool parameterOmitted;

	void PrepareToReadNextParam();
	void ProcessByte(unsigned char value);
	void FlushOutText();
	void FireAnsiCommand(unsigned char b);
	void InitializeLetterToActionMapping();
};